<template>
  <div class="app-container">
    <el-button
      size="small"
      style="margin: 10px 0"
      class="filter-item"
      type="primary"
      v-if="hasPermission('system:menu:add')"
      icon="el-icon-plus"
      @click="dialog = true,formMenu={}"
    >
      添加
    </el-button>
    <el-table
      :data="this.tableData"
      border
      size="small"
      row-key="id"
      :indent="retraction"
      :default-expand-all=false
    >
      <el-table-column align="center" width="100px" />
      <el-table-column
        prop="name"
        align="center"
        width="150px"
        label="菜单名称"
      />
      <el-table-column prop="path" align="center" label="菜单路径" />
      <el-table-column align="center" label="菜单图标">
        <template #default="scope">
          <i :class="scope.row.icon"></i>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button
            v-if="scope.row.type === 0&&hasPermission('system:menu:add')"
            @click="addChildrenMenu(scope.row.id)"
            type="primary"
            size="small"
            >新增子菜单
          </el-button>
          <el-button
            v-else-if="hasPermission('system:menu:add')"
            @click="addChildrenMenu(scope.row.id)"
            type="primary"
            size="small"
            disabled
            >新增子菜单
          </el-button>
          <el-button @click="menuEdit(scope.row)" v-if="hasPermission('system:menu:edit')" size="small" type="warning"
            >修改</el-button
          >
          <el-button
            type="danger"
            size="small"
            v-if="hasPermission('system:user:delete')"
            @click="menuDelete(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 弹出框 -->
    <el-dialog style="margin-top:-8vh" center title="编辑菜单" :visible="dialog" width="50%">
      <el-form :model="formMenu" class="demo-form-inline" label-width="100px">
        <el-form-item label="菜单名称">
            <el-input
            type="text"
            v-model="formMenu.name"
            placeholder="请输入菜单名"
            />
        </el-form-item>
        <el-form-item label="菜单路径">
            <el-input
          type="text"
          v-model="formMenu.path"
          placeholder="请输入路径"
        />
        </el-form-item>
        <el-form-item label="菜单图标">
            <el-input
          type="text"
          v-model="formMenu.icon"
          placeholder="请输入图标"
        />
        </el-form-item>
        <el-form-item label="菜单类型">
            <el-input
          type="text"
          v-model="formMenu.type"
        />
        </el-form-item>
        <el-form-item label="菜单权限">
            <el-input
          type="text"
          v-model="formMenu.permissionValue"
        />
        </el-form-item>
        <el-form-item label="组件路径">
            <el-input
          type="text"
          v-model="formMenu.component"
        />
        </el-form-item>
        <el-form-item label="重定向路径">
            <el-input
          type="text"
          v-model="formMenu.redirect"
        />
        </el-form-item>
      </el-form>

      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialog = false">取消</el-button>
          <el-button
            type="primary"
            @click="
              dialog = false;
              menuInsertAndUpdate();
            "
            >确认</el-button
          >
        </span>
      </template>
    </el-dialog>
  </div>
</template>
<script>
import {getMenu, addOrUpdate, deleteMenu } from "@/api/menu";
export default {
  data() {
    return {
      tableData: [],
      dialog: false,
      retraction: 10,
      formMenu: {
        name: "",
        path: "",
        icon: "",
        type:'',
        pid: "",
        permissionValue:'',
        component:'',
        redirect:''
      },
    };
  },
  methods: {
    // 获取菜单
    getMenu() {
      getMenu().then((response) => {
        this.tableData = response.data.item;
      });
    },
    menuInsertAndUpdate() {
      this.dialog = false;
      addOrUpdate(this.formMenu).then(() => {
        this.getMenu();
      });
      if (this.formMenu.id) {
        this.$message({ message: "修改成功！！", type: "success" });
      } else {
        this.$message({ message: "添加成功！！", type: "success" });
      }
    },
    addChildrenMenu(pid) {
        this.formMenu={};
        this.dialog = true;
        this.formMenu.pid = pid;
    },
    menuEdit(row) {
      this.dialog = true;
      this.formMenu = row;
    },
    menuDelete(id) {
      this.$confirm("确实要删除吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        deleteMenu(id).then(() => {
          this.$message({
            type: "success",
            message: "删除成功!",
          });
          this.getMenu();
        });
      });
    },
  },
  created() {
    this.getMenu();
  },
};
</script>